<?php
declare (strict_types = 1);

namespace app;

use app\model\UserModel;
use think\App;
use think\exception\ValidateException;
use think\route\dispatch\Controller;
use think\Validate;
use think\facade\Request;

/**
 * 控制器基础类
 */
abstract class ApiController extends BaseController
{
    public $loginUser;
    /**
     * Request实例
     * @var \think\Request
     */
    protected $request;

    /**
     * 应用实例
     * @var \think\App
     */
    protected $app;

    /**
     * 是否批量验证
     * @var bool
     */
    protected $batchValidate = false;

    /**
     * 控制器中间件
     * @var array
     */
    protected $middleware = [];

    // 初始化
    protected function initialize()
    {
        $baseUrl = Request::baseUrl();
        $baseUrlList = array(
            '/api/login',
            '/api/regist',
        );
        //进行登录判断
        $token = empty($_REQUEST['token']) ? '' : $_REQUEST['token'];
        if(!empty($token)){
            $userModel = new UserModel();
            $user = $userModel->getByToken($token);
            if(!empty($user)){
                $this->loginUser = $user;
            }
        }
        if(!in_array($baseUrl,$baseUrlList)){
            if(empty($this->loginUser)){
                return $this->echoOk(array(),'未登录');
            }
        }
    }

}
